﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace MISAPP.Classify.DataAccess
{
    public class Utility:DA_Matrix
    {        
        public static object CreateObject(string p_className)
        {
            Type temp = Type.GetType(string.Format("MISAPP.Classify.DataAccess.{0}", p_className));
            return System.Activator.CreateInstance(temp);
        }

        public static System.Reflection.MethodInfo FindMethodInfo(
            System.Reflection.MethodInfo[] p_mi, string p_methodName
            , Type[] p_paraTypes     )
        {
            bool ok = true;
            System.Reflection.MethodInfo rtnMethod = null;
            foreach (System.Reflection.MethodInfo m in p_mi)
            {
                if (m.Name.ToLower().Equals(p_methodName.ToLower()))
                {
                    ok = true;
                    System.Reflection.ParameterInfo[] paraInfo = m.GetParameters();
                    if (p_paraTypes == null)
                    {
                        rtnMethod = m;
                        break;
                    }
                    if (paraInfo.Length == p_paraTypes.Length)
                    {
                        for (int i = 0; i < paraInfo.Length; i++)
                        {
                            //確認型別都是我要的
                            if (paraInfo[i].ParameterType != p_paraTypes[i])
                            {
                                ok = false;
                                break;
                            }
                        }
                        rtnMethod = (ok == true) ? m : null;
                        if (rtnMethod != null) { break; }
                    }

                }
            }
            return rtnMethod;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="p_paraTeam">MISParameter.team</param>
        /// <param name="p_level"></param>
        /// <returns></returns>
        public static DataTable GetParameterByLevel(string p_paraTeam,string p_level)
        {
            DA_MISParameter para = new DA_MISParameter();
            DataTable dt = para.GetParameter(p_paraTeam, p_level);
            return dt;
        }


        public static string TheYearFirstDay(string p_year)
        {
            //如果轉不成功，就用現在的Year。
            string sDate = string.Format("{0}-01-01", p_year);
            try
            {
                Convert.ToDateTime(sDate);
            }
            catch (Exception ex)
            {
                sDate = string.Format("{0}-01-01"
                    ,DateTime.Now.Year.ToString());
            }
            return sDate;
        }

        /// <summary>
        /// 如果是給2011，回傳2011-12-31
        /// </summary>
        /// <param name="p_year"></param>
        /// <returns></returns>
        public static string TheYearLastDay(string p_year)
        {
            string sDate = Utility.TheYearFirstDay(p_year);
            try
            {
                DateTime lastYearDay = Convert.ToDateTime(sDate).AddYears(1).AddDays(-1);
                sDate = string.Format("{0}-12-31", lastYearDay.Year.ToString());
            }
            catch (Exception ex)
            {
                sDate = string.Format("{0}-12-31"
                    , DateTime.Now.Year.ToString());
            }
            return sDate;
        }

        public static string GetYYYYMM(DateTime p_d1)
        {
            string y, m;
            y = p_d1.Year.ToString();
            m = string.Format("0{0}", p_d1.Month.ToString());
            m = m.Length == 2 ? m : m.Substring(1);
            return string.Format("{0}{1}",y,m);        
        }

        public static string GetYearMonthString(string[] p_months, string p_str)
        {
            StringBuilder sb = new StringBuilder();
            foreach (string mon in p_months)
            {
                sb.AppendFormat(p_str, mon);
            }

            return sb.ToString();
        }

        public static string GetCIBDetailField(string p_alias)
        {
            return string.Format(@"
                        {0}[YYYYMM]
                        ,dbo.ReplaceCIBValue({0}[帳號],2)帳號
                        ,ISNULL([透支帳號],'') AS [透支帳號]
                        ,[指標利率]
                        ,[利率說明]
                        ,[均額]
                        ,[餘額]
                        ,[CUR]
                        ,[承作利率]
                        ,[FTP_RATE]
                        ,[利差]
                        ,dbo.ReplaceCIBValue([LNAC_NAME],1)LNAC_NAME
                        ,[利率調整型態]
                        ,[專案代碼]
                        ,[專案名稱]
                        ,[加減碼]
                        ,[起息日]
                        ,[到期日]
                        ,[信用評等]
                        ,[信用評分]
                        ,[風險等級]
                        ,[評等類別]
                        ,[授信行業別代碼]
                        ,[授信行業別]
                        ,[核准額度]
                        ,[調息週期]
                        ,[循環別]
                        ,[擔保品鑑價值]
                        ,[擔保品代碼1]
                        ,[擔保品名稱1]
                        ,ISNULL([擔保品代碼2],'') AS [擔保品代碼2]
                        ,ISNULL([擔保品名稱2],'') AS [擔保品名稱2]
                        ,ISNULL([違約狀態],'') AS [違約狀態]
                        ,[會計科目]
                        ,[科目名稱]
                        ,ISNULL([本金逾期代號],'') AS [本金逾期代號]
                        ,[本金逾期金額]
                        ,ISNULL([利息逾期代號],'') AS [利息逾期代號]
                        ,[利息逾期金額]", p_alias) ;
        }

        /// <summary>
        /// 把變數加上雙引號
        /// </summary>
        /// <param name="p_vals"></param>
        /// <returns></returns>
        public static string GetDoubleQuoteString(string[] p_vals)
        {
            StringBuilder sb = new StringBuilder();
            foreach (string vl in p_vals)
            {
                sb.AppendFormat(",!{0}!",vl);
            }
            return sb.ToString().Substring(1).Replace('!','"');
        }

        public static DataTable GetComplexlistboxTable(string p_tableName
            ,string[] p_contents,string p_columnName)
        {
            DataTable dt = new DataTable(p_tableName);
            dt.Columns.Add(p_columnName);
            foreach (string content in p_contents)
            {
                DataRow row = dt.NewRow();
                row[0] = content;
                dt.Rows.Add(row);
            }
            return dt;
        }

        public static DataTable GetYYYYMMRange(DateTime p_start)
        {
            System.Collections.ArrayList ymList = new System.Collections.ArrayList();
            DateTime startDate,endDate = DateTime.Now.AddMonths(-1);
            startDate = p_start;
            string mm, yyyy;            
            while (startDate <= endDate)
            {
                mm = string.Format("0{0}", endDate.Month.ToString());
                mm = mm.Length > 2 ? mm.Substring(1) : mm;
                yyyy = string.Format("{0}", endDate.Year.ToString());
                ymList.Add(string.Format("{0}{1}", yyyy, mm));
                endDate = endDate.AddMonths(-1);
            }
            return Utility.GetComplexlistboxTable("YYYYMM",
                (String[])ymList.ToArray(typeof(string)), "YYYYMM");
        }

        public static DataTable GetCIBAccountNoData()
        {
            return Utility.GetComplexlistboxTable("會計科目"
                ,new string[]  {  "1812" ,"1361"
                        ,"1351"  ,"1341"
                        ,"1331"  ,"1322"
                        ,"1321"  ,"1313"
                        ,"1311"  ,"1304"
                        ,"1144" }
                , "會計科目");
        }

        public static DataTable GetCIBCurData()
        {
            return Utility.GetComplexlistboxTable("CUR",
                new string[] { "USD","TWD","SGD","SEK"
                            ,"NZD","JPY","HKD","GBP"
                            ,"EUR","CNY","CHF","CAD","AUD"},
                "CUR");
        }

        public static DataTable GetCIBRateAdjType()
        {
            return Utility.GetComplexlistboxTable("利率調整型態"
                , new string[] { "V", "F", "A" }
                , "利率調整型態");
        }
        
    }
}
